<?php
/**
 * @version		$Id: example.php 8300 2007-08-02 00:41:40Z jinx $
 * @package		Joomla
 * @subpackage	Content
 * @copyright	Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */

// Check to ensure this file is included in Joomla!
defined( '_JEXEC' ) or die();

jimport( 'joomla.event.plugin' );

/**
 * Example Content Plugin
 *
 * @package		Joomla
 * @subpackage	Content
 * @since 		1.5
 */
class plgSystemBookstore extends JPlugin
{

	/**
	 * Constructor
	 *
	 * For php4 compatability we must not use the __constructor as a constructor for plugins
	 * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
	 * This causes problems with cross-referencing necessary for the observer design pattern.
	 *
	 * @param object $subject The object to observe
	 * @param object $params  The object that holds the plugin parameters
	 * @since 1.5
	 */
	function plgSystemBookstore( &$subject, $config)
	{
		parent::__construct( $subject, $config);
		
	}

	/**
	 * Example prepare content method
	 *
	 * Method is called by the view
	 *
	 * @param 	object		The article object.  Note $article->text is also available
	 * @param 	object		The article params
	 * @param 	int			The 'page' number
	 */
	function onAfterRoute()
	{
		global $mainframe;
		/**
		* normally, the plugin is for frontside only,
		* i'm commented it so that we can get the image path
		*  (saves a http request when looking at event details)
		if ($mainframe->isAdmin()) {
			return;
		}
		*/
		/////// check JRequest for 'option' components that can utilze this function ///////
		// first get list of components that are set to use this API
		$db =& JFactory::getDBO();
		$query = 'SELECT `params`'
		. ' FROM #__plugins'
		. ' WHERE element=' . $db->quote( bookstore );
		$db->setQuery( $query );
		$results = $db->loadResultArray();
		
		
		if (!empty($results)) {
			//get plugin parameters
			$params = new JParameter('');
			$params->def('global_api',0);
			$params->def('components','');
			$params->loadINI($results[0]);
			$global_api = $params->get('global_api');
			$com_array = explode(",",$params->get('components'));
			function com_trim_value(&$value){$value = trim($value);}
			array_walk($com_array,'com_trim_value');
			if( (count($com_array) > 0) || (!empty($global_api)) ){
				$option = JRequest::getVar('option');
				// second determine if user has requested a component in our list	
				if( in_array($option,$com_array) ){
					// if yes, load class library; no, exit 
					$file = JPATH_SITE.DS.'components'.DS
					//.$option
					.'com_bookstore'
					.DS
					.'helpers'.DS.'api.php';
					
					if (file_exists( $file )) {
						require_once( $file );
						//experimental global store of api mode; should be faster that db call in controller?
						$config = &JFactory::getConfig();
						$config->setValue('config.bookstore_api', 1);	
					}//end if file_exists
					
					//for query logging
					//$this->logQuery();
					
				}//end if in array
			}//end items in array		
		}//end if results		
		//  with class defined, the controller will succeed in creating the api object(which is attempted when plugin is enabled) 
		
			
	}//end method onAfterRoute	
	
	function logQuery(){
		global $mainframe;
		$db =& JFactory::getDBO();
		include_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_bookstore'.DS.'tables'.DS.'bookstorequerylog.php');
		$row = new TableBookstorequerylog($db);
		$data = JRequest::getVar( 'terms' );
		$query_type = JRequest::getVar('query_type');
		$orig_terms = $mainframe->getUserState( "com_bookstore.query.orig_terms" );
		$reg = new JRegistry('querylog');
		$reg->loadArray($orig_terms);
				
		if($query_type == 'Search'){
			$data['offset'] = 0;
			
		}elseif($query_type == 'Advanced Query'){
			$data['offset'] = 1;
			
		}elseif($query_type == 'pageNav'){
			$data = array();
			$data[] = $orig_terms;	
			$data['offset'] = 3;
		}else{
			$data['offset'] = 4; //no search, just landing
		}
		$old = $reg->toString();
		$data['previous'] =  $reg->toString();
		jimport('joomla.utilities.date');
		$jnow = new JDate();
		$data['querytime'] = $jnow->toMySQL();
	
		if (!$row->bind($data)) {
			//$this->setError($this->_db->getErrorMsg());
			//return false;
		}
		
		// Make sure the hello record is valid
		if (!$row->check()) {
		//	$this->setError($this->_db->getErrorMsg());
		//	return false;
		}
	
		// Store the web link table to the database
		if (!$row->store()) {
		//	$this->setError($this->_db->getErrorMsg());
		//	return false;
		}
		
	}//end logQuery method
}//end plgSystemBookstore class